const SIGN_REGEXP = /([yMdhsm])(\1*)/g
const DEFAULT_PATTERN = 'yyyy-MM-dd'
function padding (s, len) {
  len = len - (s + '').length
  for (var i = 0; i < len; i++) { s = '0' + s }
  return s
}

let util = {
  dateFtt: function (fmt, dateParms) {
    let date = new Date(dateParms)
    let o = {
      'M+': date.getMonth() + 1, // 月份
      'd+': date.getDate(), // 日
      'h+': date.getHours(), // 小时
      'm+': date.getMinutes(), // 分
      's+': date.getSeconds(), // 秒
      'q+': Math.floor((date.getMonth() + 3) / 3), // 季度
      'S': date.getMilliseconds() // 毫秒
    }
    if (/(y+)/.test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (date.getFullYear() + '').substr(4 - RegExp.$1.length))
    }
    for (var k in o) {
      if (new RegExp('(' + k + ')').test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? (o[k]) : (('00' + o[k]).substr(('' + o[k]).length)))
      }
    }
    return fmt
  },
  // 将中国标准时间转换为14位日期格式
  /**
   * @param {*} ms 中国标准时间
   * @param {*} val 判断返回格式
   */
  getDateTime: (ms, val) => {
    let Y = ms.getFullYear()
    let M = ms.getMonth() + 1
    if (M < 10) {
      M = '0' + M
    }
    let D = ms.getDate()
    if (D < 10) {
      D = '0' + D
    }
    let h = ms.getHours()
    if (h < 10) {
      h = '0' + h
    }
    let m = ms.getMinutes()
    if (m < 10) {
      m = '0' + m
    }
    let s = ms.getSeconds()
    if (s < 10) {
      s = '0' + s
    }
    if (val === 's') {
      return '' + Y + M + D + h + m + s
    } else if (val === 'ss') {
      return Y + '-' + M + '-' + D + ' ' + h + ':' + m + ':' + s
    } else if (val === 'day') {
      return Y + '-' + M + '-' + D
    } else {
      return Y + '-' + M + '-' + D + ' ' + h + ':' + m
    }
  },
  // 毫秒数获取当前时间，格式YYYY-MM-DD
  getNowFormatDate: (val) => {
    let date = new Date(val)
    let seperator1 = '-'
    let year = date.getFullYear()
    let month = date.getMonth() + 1
    let strDate = date.getDate()
    if (month >= 1 && month <= 9) {
      month = '0' + month
    }
    if (strDate >= 0 && strDate <= 9) {
      strDate = '0' + strDate
    }
    return year + seperator1 + month + seperator1 + strDate
  },
  randomColor: function () {
    let colorArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c', 'd', 'e', 'f']
    let color = '#'
    for (let i = 0; i < 6; i++) {
      color = color + colorArray[Math.floor(Math.random() * 16)]
    }
    return color
  },

  getQueryStringByName: (name) => {
    let reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i')
    let r = window.location.search.substr(1).match(reg)
    let context = ''
    if (r != null) { context = r[2] }
    reg = null
    r = null
    return context == null || context === '' || context === 'undefined' ? '' : context
  },
  formatDate: {
    format: (date, pattern) => {
      pattern = pattern || DEFAULT_PATTERN
      return pattern.replace(SIGN_REGEXP, function ($0) {
        switch ($0.charAt(0)) {
          case 'y': return padding(date.getFullYear(), $0.length)
          case 'M': return padding(date.getMonth() + 1, $0.length)
          case 'd': return padding(date.getDate(), $0.length)
          case 'w': return date.getDay() + 1
          case 'h': return padding(date.getHours(), $0.length)
          case 'm': return padding(date.getMinutes(), $0.length)
          case 's': return padding(date.getSeconds(), $0.length)
        }
      })
    },
    parse: (dateString, pattern) => {
      const matchs1 = pattern.match(SIGN_REGEXP)
      const matchs2 = dateString.match(/(\d)+/g)
      if (matchs1.length === matchs2.length) {
        const _date = new Date(1970, 0, 1)
        for (let i = 0; i < matchs1.length; i++) {
          const _int = parseInt(matchs2[i])
          const sign = matchs1[i]
          switch (sign.charAt(0)) {
            case 'y': _date.setFullYear(_int); break
            case 'M': _date.setMonth(_int - 1); break
            case 'd': _date.setDate(_int); break
            case 'h': _date.setHours(_int); break
            case 'm': _date.setMinutes(_int); break
            case 's': _date.setSeconds(_int); break
          }
        }
        return _date
      }
      return null
    }
  },
  /**
   * 日期格式化
   */
  formatDateTime: (data) => {
    if (data) {
      return data.replace(/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/, '$1-$2-$3 $4:$5:$6')
    }
  }
}
export default util
